# deep_model/tests.py
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from .models import ImageModel

class ImageUploadTests(APITestCase):
    def test_image_upload(self):
        # 定义图像上传端点的 URL
        url = reverse('upload_image')

        # 打开一个用于测试的图像文件
        with open('test/test.py测试脚本用的.jpg', 'rb') as image_file:
            # 定义要发送的请求数据
            data = {'image': image_file}

            # 发送 POST 请求到图像上传端点
            response = self.client.post(url, data, format='multipart')

        # 检查响应状态码是否为 201 CREATED
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # 检查图像是否已保存到数据库
        self.assertEqual(ImageModel.objects.count(), 1)
        self.assertEqual(ImageModel.objects.get().image.name, 'images/test.py测试脚本用的.jpg')

